//
//  LyvistorLoginView.swift
//  SinaWeibo
//
//  Created by 蒋琪 on 2017/5/28.
//  Copyright © 2017年 liuying. All rights reserved.
//

import UIKit

//MARK: 用户登录协议
@objc protocol LyVistorLoginViewDelegate:NSObjectProtocol{
    
    func userWillLogin()//登录
    
    @objc optional func userWillRegister()//注册
}


//MARK: -自定义访客视图
class LyvistorLoginView: UIView {
    
    //MARK: -代理
    weak var delegate:LyVistorLoginViewDelegate?
    
    //MARK: -对外提供设置信息的方法
    //MARK: -
    func updateInfo(tipTxt:String,imgName:String? = nil) {
        tipLbl.text = tipTxt//更新tip内容
        if imgName == nil {//判断如果是首页,执行动画效果
            
            startAnimation()
            //0x60800002f140 0x7f84a2f0aa20
        }else{//不是首页
            print("不是首页")
            
            print(tipTxt)
            print(imgName!)
            circleView.image = UIImage(named: imgName!)
            iconView.isUserInteractionEnabled = true
            iconView.isHidden = true//隐藏iconView大房子
            bringSubview(toFront: circleView)
            print(circleView)
        }
    }
    
    //MARK: -圈圈的动画效果
    //MARK: -
    private func startAnimation(){
        
        //使用基础动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.repeatCount = MAXFLOAT    //无限重复
        animation.toValue = Double.pi * 2   //一圈
        animation.duration = 15             //动画时间15s
        animation.isRemovedOnCompletion = false //当动画结束的时候/页面处于非活跃状态,移除动画效果
        
        circleView.layer.add(animation, forKey: nil)
    }
    
    
    //MARK: -初始化方法,添加子视图
    //MARK: -
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    //解归档实现默认报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: 设置UI界面
    //MARK: -
    private func setupUI(){
        
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        addSubview(tipLbl)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //布局
        circleView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
        }
        iconView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        backView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        tipLbl.snp.makeConstraints { (make) in
            make.top.equalTo(circleView.snp.bottom).offset(16)
            make.centerX.equalTo(circleView)
        }
        loginBtn.snp.makeConstraints { (make) in
            make.left.equalTo(tipLbl)
            make.top.equalTo(tipLbl.snp.bottom).offset(16)
            make.width.equalTo(100)
        }
        registerBtn.snp.makeConstraints { (make) in
            make.right.equalTo(tipLbl)
            make.top.equalTo(tipLbl.snp.bottom).offset(16)
            make.width.equalTo(100)
        }
        
        
        let value = CGFloat(237/255.0)
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)//设置背景色
        loginBtn.addTarget(self, action: #selector(loginBtnDidClick), for: .touchUpInside)
        registerBtn.addTarget(self, action: #selector(reigsterBtnDidClick), for: .touchUpInside)
        
    }
    
    //MARK: -懒加载子视图
    //MARK: -
    //转动的圈圈
    private lazy var circleView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    //大房子
    private lazy var iconView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    //遮罩
    private lazy var backView:UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    //提示文字
    private lazy var tipLbl:UILabel = {
        let l = UILabel(title: "hello,world!Are you OK?ello,world!Are you OK?ello,world!Are you OK?", txtColor: UIColor.darkGray, fontSize: 14)
        //换行:1.限制宽度,2.设置自动换行
        l.numberOfLines = 0
        l.preferredMaxLayoutWidth = 224//设置最大的布局宽度,如果用snp的话会换行不准确
        l.textAlignment = NSTextAlignment.center//设置居中对齐
        l.isUserInteractionEnabled = true
        return l
        
    }()
    
    //注册按钮
    private lazy var registerBtn:UIButton = {
        
        let btn = UIButton(title: "注册", txtColor: .orange, fontSize: 14)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    
    //登录按钮
    private lazy var loginBtn:UIButton = {
        let btn = UIButton(title: "登录", txtColor: UIColor.darkGray, fontSize: 14)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    
    
    //MARK: -登录按钮点击事件
    //MARK: -
    @objc private func loginBtnDidClick(){
        
        delegate?.userWillLogin()
        
    }
    //MARK: -注册按钮点击事件
    //MARK: -
    @objc private func reigsterBtnDidClick(){
        
        delegate?.userWillRegister?()//必选协议方法必须要实现,可选协议可实现可不实现
        
    }
    
    
    
    
    
    
}
